// 引入正则
const { phoneCheck, idCardCheck, normalCheck, dateCheck, timeCheck, numberCheck } = require("./regModel")


// 登录公共检测类
const commonCheck = (key, val) => {
    let CheckRes;
    switch (key) {
        case "phone":
        case "tel":
        case "telphone":
        case "phoneNumber":
            CheckRes = phoneCheck(val);
            break;

        case "idcard":
            CheckRes = idCardCheck(val);
            break;

        case "date":
            CheckRes = dateCheck(val);
            // 检测日期
            break;

        case "startTime":
        case "endTime":
            CheckRes = timeCheck(val);
            // 检测时间
            break;

        case "allNum":
        case "num":
        case "number":
            CheckRes = numberCheck(val);
            break;
        // 检测数字
        
        default:
            CheckRes = normalCheck(val)
            // 检测名字
            break;
    }
    return CheckRes;
}



// 判断接口所需要的值是否符合要求

// ...targetArr => reset 参数
const checkData = (req, ...targetArr) => {
    //全部参数正确 返回true
    // 检测到其中一项不正确返回字符串类型  exp:"phone不正确"
    let isPass = true;
    let dataObj;

    if (req.method === "GET") {
        dataObj = req.query;
    } else {
        dataObj = req.body;
    }
    // console.log(dataObj);
    // dataObj是请求req.body的数据  参数对象{ name: 'melon', phone: 13751704638 }
    // console.log(targetArr);
    // 是userRouter.js的三个必填参数，检测对象的数组[ 'name', 'phone', 'idcard' ]

    targetArr.forEach(item => {
        // dataObj[item]检测是否存在
        if (!(dataObj[item] && commonCheck(item, dataObj[item]))) {
            isPass = item;
        }
    })
    return isPass;
}

module.exports = checkData;